<template>

	<swiper class="swiper" vertical :duration="300">
		<swiper-item>
			<view class="content" :style="{ backgroundImage: 'url(' + bgUrl + ')' }">
				<!-- 上半部分：标题和副标题 -->
				<view class="top-section">
					<view class="main-title">{{ title }}</view>
					<view class="subtitle">{{ subtitle }}</view>
					<view class="meetingCount" v-if="issuePattern=='2'"></view>
					<view class="meetingCount" v-else>（共{{total}}项议题）
						<view class="meetingCount" style="padding-top: 12px;" v-if="grouping=='4'">(预览版)</view>
					</view>
				</view>
				<view class="bottom-section">
					<view class="bottom-info">中共宜昌市委办公室</view>
					<view class="bottom-info">{{ currentDate }}</view>
				</view>
			</view>
		</swiper-item>
		<swiper-item v-if="issuePattern=='2'" v-for="(notice, index) in noticeChildren">
			<view>
				<image style="width: 100%;" :src="notice.name" mode="widthFix"></image>
			</view>
			<view class="bottom_tool" v-if="isList==1">
				<view class="tool_box">
					<button type="warn" size="mini" @click="conferenceList">会议列表</button>
					<!-- <button type="warn" size="mini" @click="back">返回上级</button> -->
					<!-- <button type="warn" size="mini" @click="jumpPage">文件首页</button> -->
				</view>
			</view>
		</swiper-item>
		<swiper-item v-if="issuePattern=='2'">
			<uni-nav-bar dark :fixed="true" shadow background-color="rgb(255, 0, 0)" status-bar left-icon="left"
				left-text="返回" :title="navbarTitle" @clickLeft="back" />
			<view class="pdf-list-page">
				<view class="pdf-grid">
					<view class="pdf-card" v-for="(pdf, index) in pdfFiles" :key="index" @click="openPdf1(pdf)">
						<view class="pdf-icon" mode="aspectFit"></view>
						<view class="pdf-name">{{ pdf.name }}</view>
						<view class="empty" v-if="!pdfFiles || pdfFiles.length == 0">
							<text class="empty-text">没有任何文件</text>
						</view>
					</view>
				</view>
				<view class="bottom_tool" v-if="isList==1">
					<view class="tool_box">
						<button type="warn" size="mini" @click="conferenceList">会议列表</button>
						<!-- <button type="warn" size="mini" @click="back">返回上级</button> -->
						<!-- <button type="warn" size="mini" @click="jumpPage">文件首页</button> -->
					</view>
				</view>
			</view>
		</swiper-item>
		<swiper-item v-else>
			<view class="metting-warp">
				<view class="warp-title">会 议 议 题</view>
				<view class="meetingCount2">（共{{total}}项议题）</view>
				<scroll-view scroll-y style="height: 80vh;" :scroll-with-animation="true" :enable-back-to-top="true">
					<view class="warp-content">
						<view class="file-item" v-for="(item, index) in list" :key="index">
							<view class="file-item_title" @tap="showPasswordDialog(item)">
								{{index + 1}}.{{ item.name }}
								<span v-if="item.reportName">({{item.reportName}}汇报)</span>
							</view>
							<view class="file-item_subtitle" v-if="item.participant">
								列席：{{ item.participant }}
							</view>
						</view>
					</view>
					<view class="bottom_tool" v-if="isList==1">
						<view class="tool_box">
							<button type="warn" size="mini" @click="conferenceList">会议列表</button>
							<!-- <button type="warn" size="mini" @click="back">返回上级</button> -->
							<!-- <button type="warn" size="mini" @click="jumpPage">文件首页</button> -->
						</view>
					</view>
				</scroll-view>
			</view>
		</swiper-item>
	</swiper>
</template>

<script>
	import bg from '@/static/bg4.png';
	import FileJS from "@/utils/File.js";
	import uniNavBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue';
	export default {
		data() {
			return {
				bgUrl: bg,
				title: '', // 宜 昌 市 委 无 纸 化 办 公 系 统
				subtitle: '', // 宜昌市委常委会会议第x次会议
				issuePattern: '',
				grouping: '',
				total: null,
				list: [], // '内容1', '内容2'
				currentDate: this.getFormattedDate(),
				current: '',
				navbarTitle: '',
				topicName: '',
				meetId: '',
				// 假设文件都在 /ycmlMeeting 文件夹中
				pdfFiles: [],
				noticeChildren: [],
				filekey: '',
				isList: '',
				sdRootPath: '/storage/emulated/0'
			};
		},
		onLoad(options) {
			plus.device.setWakelock(true);
			if (options.meetData != null) {
				this.isList = options.isList
				this.readFN2(options.meetData)
			}
		},
		onUnload() {
			// 🔴 页面卸载时释放唤醒锁
			plus.device.setWakelock(false);
		},
		mounted() {
			// 可选：每秒更新一次时间（如果需要动态更新）
			setInterval(() => {
				this.currentDate = this.getFormattedDate();
			}, 1000);
		},
		methods: {
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			conferenceList() {
				uni.redirectTo({
					url: '/pages/index/main',
					animationType: 'none',
					animationDuration: 0
				})
			},
			jumpPage() {
				this.pages.current = 1
				this.$refs.pdfview.jumpTo({
					// 页码
					page: Number(this.pages.current),
					// 是否开启动画
					animation: false
				});
			},
			openPdf1(pdf) {
				console.log("pdf", pdf)
				if (!this.navbarTitle) return
				if (!pdf.name) return
				if (this.grouping == '3') {
					if (pdf.classified == '1') {
						uni.showToast({
							title: '涉密文件，无权限查看！',
							icon: 'none',
							duration: 2000
						})
					} else {
						this.viewPdf(pdf);
					}
				} else {
					this.viewPdf(pdf);
				}
			},

			viewPdf(pdf) {
				const filePath =
					`${this.sdRootPath}/wzhxt_public/unzipped/ycmlMeeting/${this.meetId}/${this.topicName}/${pdf.filename}`
				const isList = this.isList
				plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
					if (entry.isFile) {
						uni.navigateTo({
							url: '/pages/pdfPreview/index?path=' + encodeURIComponent(`${filePath}`) +
								'&isList=' + isList,
							"animationType": "slide-in-bottom"
						})
					} else {
						uni.showToast({
							title: '无详情',
							icon: 'none'
						})
					}
				}, function(err) {
					uni.showToast({
						title: '无详情',
						icon: 'none'
					})
				});
			},
			readFN2(data) {
				try {
					let jsonData = JSON.parse(data);
					// 获取文件路径
					const filePath = this.sdRootPath + "/wzhxt_public/text_files/meets.txt";
					const content = JSON.parse(FileJS.readTxt(filePath));

					for (let i in content) {
						if (content[i].meetId == jsonData.meetId) {
							this.grouping = content[i].deviceGrouping
						}
					}

					this.title = jsonData.title
					this.issuePattern = jsonData.issuePattern
					this.meetId = jsonData.meetId
					this.subtitle = jsonData.subTitle
					this.total = jsonData.total
					this.list = jsonData.content
					this.noticeChildren = jsonData.noticeChildren
					for (let index in this.noticeChildren) {
						this.noticeChildren[index].name = this.sdRootPath + '/wzhxt_public/unzipped/ycmlMeeting/' + this
							.meetId + '/notice/' + this.noticeChildren[index].name
					}

					this.topicName = jsonData.content[0].filename
					this.navbarTitle = jsonData.content[0].name
					this.filekey = jsonData.content[0].filekeys;
					this.pdfFiles = jsonData.content[0].children

					let path = this.sdRootPath + '/wzhxt_public/unzipped/ycmlMeeting/' + this.meetId + '/' + jsonData
						.backimg;
					if (jsonData.backimg) {
						this.bgUrl = 'file://' + path;
					}
				} catch (e) {
					console.error("JSON 解析失败：", e);
					uni.showToast({
						title: '加载失败，请检查',
					})
				}

			},
			openPdf(item, subitem) {
				if (!item.filename) return
				if (!subitem.filename) return
				const filePath =
					`${this.sdRootPath}/wzhxt_public/unzipped/ycmlMeeting/${this.meetId}/${item.filename}/${subitem.filename}`
				const isList = this.isList
				plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
					console.log('文件存在', entry.isFile);
					if (entry.isFile) {
						uni.navigateTo({
							url: '/pages/pdfPreview/index?path=' + encodeURIComponent(`${filePath}`) +
								'&isList=' + isList,
						})
					} else {
						uni.showToast({
							title: '无详情',
							icon: 'none'
						})
					}
				}, function(err) {
					uni.showToast({
						title: '无详情',
						icon: 'none'
					})
				});
			},

			openBokeHuiyiFolder(item) {
				const isList = this.isList
				const grouping = this.grouping
				if (item.children != null && item.children.length === 1) {
					if (!item.filename) return
					const filePath =
						`${this.sdRootPath}/wzhxt_public/unzipped/ycmlMeeting/${this.meetId}/${item.filename}/${item.children[0].filename}`
					plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
						if (entry.isFile) {
							uni.navigateTo({
								url: '/pages/pdfPreview/index?path=' + encodeURIComponent(
										`${filePath}`) +
									'&isList=' + isList,
							})
						} else {
							uni.showToast({
								title: '无详情',
								icon: 'none'
							})
						}
					}, function(err) {
						uni.showToast({
							title: '无详情',
							icon: 'none'
						})
					});
				} else {
					uni.navigateTo({
						url: '/pages/folder/index?file=' + JSON.stringify(item) + "&meetId=" + this.meetId +
							'&isList=' + isList + '&grouping=' + grouping,
					})
				}
			},
			//点击大标题
			showPasswordDialog(item) {
				if (item.children == null || item.children.length === 0) {
					uni.showToast({
						title: '此议题不印发文件',
						icon: 'none',
						duration: 2000
					})
				} else {
					if (this.grouping == "1") {
						this.openBokeHuiyiFolder(item);
					} else if (this.grouping == "3" && item.children != null) {
						if (item.children.length === 1) {
							if (item.children[0].classified == '0') {
								this.openBokeHuiyiFolder(item);
							} else if (item.children[0].classified == '1') {
								uni.showToast({
									title: '涉密文件，无权限查看！',
									icon: 'none',
									duration: 2000
								})
							}
						} else if (item.children.length > 1) {
							this.openBokeHuiyiFolder(item);
						}
					} else if (this.grouping == "2") {
						uni.showModal({
							title: '请输入密码',
							content: '',
							editable: true,
							placeholderText: '请输入密码',
							inputType: 'number',
							cancelText: '取消',
							confirmText: '确认',
							success: (res) => {
								if (res.confirm) {
									const inputPassword = res.content.trim();
									this.verifyPassword(inputPassword, item);
								}
							}
						});
					}
				}
			},
			verifyPassword(inputPassword, item) {
				// 假设你的密码存储在某个json对象中，这里用示例代码
				// 实际使用时需要替换为你的真实密码获取方式

				// 示例：从本地存储获取密码
				const correctPassword = item.filekeys // 你需要实现这个方法

				if (inputPassword === correctPassword) {
					// 密码正确，跳转到PDF预览页面
					this.openBokeHuiyiFolder(item);

				} else {
					// 密码错误，显示提示
					uni.showToast({
						title: '密码错误，请重新输入',
						icon: 'none',
						duration: 2000
					})

					// // 可以重新显示密码输入框
					// setTimeout(() => {
					//   this.showPasswordDialog(item);
					// }, 1500);
				}
			},
			//显示密码输入弹框---小标题
			showopenPdf(item, subitem) {
				if (this.grouping == "1") {
					this.openPdf(item, subitem);
				} else if (this.grouping == "3") {
					if (subitem.classified == '1') {
						uni.showToast({
							title: '涉密文件，无权限查看！',
							icon: 'none',
							duration: 2000
						})
					} else {
						this.openPdf(item, subitem);
					}
				} else if (this.grouping == "2") {
					uni.showModal({
						title: '请输入密码',
						content: '',
						editable: true,
						placeholderText: '请输入密码',
						inputType: 'number',
						cancelText: '取消',
						confirmText: '确认',
						success: (res) => {
							if (res.confirm) {
								const inputPassword = res.content.trim();
								this.verifyPassword2(inputPassword, item, subitem);
							}
						}
					});
				}
			},
			verifyPassword2(inputPassword, item, subitem) {
				// 假设你的密码存储在某个json对象中，这里用示例代码
				// 实际使用时需要替换为你的真实密码获取方式

				// 示例：从本地存储获取密码
				const correctPassword = item.filekeys // 你需要实现这个方法

				if (inputPassword === correctPassword) {
					// 密码正确，跳转到PDF预览页面
					this.openPdf(item, subitem);

				} else {
					// 密码错误，显示提示
					uni.showToast({
						title: '密码错误，请重新输入',
						icon: 'none',
						duration: 2000
					})

				}
			},
			getFormattedDate() {
				const now = new Date();
				const year = now.getFullYear();
				const month = String(now.getMonth() + 1).padStart(2, '0');
				const day = String(now.getDate()).padStart(2, '0');
				return `${year}年${month}月${day}日`;
			},
		}
	};
</script>

<style>
	@font-face {
		font-family: 'fzxbsjt';
		/* 自定义字体名称 */
		src: url('/static/fonts/fzxbsjt.ttf');
	}

	@font-face {
		font-family: 'fs';
		/* 自定义字体名称 */
		src: url('/static/fonts/fs.ttf');
	}

	@font-face {
		font-family: 'ht';
		/* 自定义字体名称 */
		src: url('/static/fonts/ht.ttf');
	}

	@font-face {
		font-family: 'kt';
		/* 自定义字体名称 */
		src: url('/static/fonts/kt.ttf');
	}
</style>

<style>
	page {
		height: 100%;
		background: #fff;
		color: rgb(38, 38, 38);
		min-height: 100vh;
		position: relative;
		overflow-x: hidden;
	}

	.swiper {
		width: 100vw;
		height: 100vh;
	}

	* {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
		font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
	}


	.content {
		display: flex;
		flex-direction: column;
		/* 垂直排列 */
		align-items: center;
		justify-content: space-between;
		/* 上下两端对齐 */
		width: 100vw;
		height: 100vh;
		background-size: cover;
		background-repeat: no-repeat;
	}

	.top-section {
		height: 60vh;
		text-align: center;
		/* 让标题居中 */
		//margin-top: 350px;
	}

	.bottom-section {
		height: 40vh;
		width: 100%;
		text-align: center;
		/* 让底部内容居中 */
		margin-bottom: 20px;
		/* 调整底部间距 */
	}

	.bottom-info {
		//font-weight: bold;
		color: black;
		line-height: 1.8rem;
		font-size: 1.8rem;
		text-align: center;
		font-family: 'kt';
		letter-spacing: 0.1em;
		margin: 18px 0;
		/* 调整行间距 */
		//padding-bottom: 50px;
	}

	// .content {
	// 	display: flex;
	// 	align-items: center;
	// 	justify-content: center;
	// 	width: 100vw;
	// 	height: 100vh;
	// 	background-size: cover;
	// 	background-repeat: no-repeat;
	// }

	.content>view {
		margin-bottom: 15rem;
	}

	.main-title {
		color: #de0112;
		font-size: 3.0rem;
		font-weight: 700;
		text-align: center;
		font-family: 'fzxbsjt';
		letter-spacing: 0.1em;
		//margin-top: 2250px; /* 增加上外边距 */
		padding-top: 300px; //增加内边距
	}

	.subtitle {
		white-space: pre-wrap;
		font-weight: bold;
		color: #de0112;
		font-size: 2.5rem;
		text-align: center;
		font-family: 'fzxbsjt';
		margin: 2.5rem 0;
		letter-spacing: 0.1em;
		/* 字间距调整 */
	}

	.meetingCount {
		//font-weight: bold;
		color: black;
		//line-height: normal;
		//margin-top: 0;        /* 上一行行高为 0 */
		margin-bottom: 45rem;
		/* 下一行行高为 45rem */
		font-size: 1.8rem;
		text-align: center;
		font-family: 'kt';
		letter-spacing: 0.1em;
	}

	// .meeting-info {
	// 	font-weight: bold;
	// 	color: black;
	// 	//line-height: normal;
	// 	//margin-top: 0;        /* 上一行行高为 0 */
	// 	margin-bottom: 1.2rem; /* 下一行行高为 45rem */
	// 	font-size: 1.8rem;
	// 	text-align: center;
	// 	font-family: 'kt';
	// 	letter-spacing: 0.1em;
	// }

	.meetingCount2 {
		//font-weight: bold;
		color: black;
		line-height: 2.5rem;
		font-size: 1.8rem;
		text-align: center;
		font-family: 'kt';
		letter-spacing: 0.1em;
		margin: 0.5rem 0 0.8rem;
	}

	.metting-warp {
		width: 100vw;
		height: 100vh;
		padding: 5rem 0
	}

	.warp-title {
		font-size: 3.5rem;
		font-weight: bold;
		text-align: center;
		color: red;
		font-family: 'fzxbsjt';
		margin-top: -0.5rem;
		/* 可选：通过负值上移标题 */
	}

	.warp-content {
		padding: 0 3.5rem 2rem 4.5rem;
	}

	.file-item_title {
		font-size: 1.5rem;
		line-height: 2.5rem;
		color: black;
		//font-weight: bold;
		font-family: "ht";
		text-indent: 2em;
	}

	.file-item_title>span {
		font-size: 1.5rem;
		color: black;
		font-weight: normal;
		font-family: "kt";
	}

	.file-item_subtitle {
		font-size: 1.5rem;
		color: black;
		// font-weight: bold;
		font-family: "fs";
		text-indent: 3.6rem;
		line-height: 2.5rem;
	}

	.pdf-list-page {
		padding: 15rpx;
		background-color: #f6f8fa;
		min-height: 100vh;
	}

	.header {
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 30rpx;
		text-align: center;
	}

	.pdf-grid {
		display: flex;
		flex-wrap: wrap;
		gap: 10rpx
			/* justify-content: space-around; */
	}

	.pdf-card {
		width: 100%;
		box-sizing: border-box;
		background-color: #ffffff;
		border-radius: 4rpx;
		box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.1);
		/* margin: 20rpx; */
		padding: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 20rpx;
		transition: transform 0.2s ease;
	}

	.pdf-card:hover {
		transform: scale(1.03);
	}

	.pdf-icon {
		width: 30rpx;
		height: 30rpx;
		/* background: url('/static/pdf.png'); */
		background-image: url('/static/pdf.png');
		background-size: cover;
		background-repeat: no-repeat;
	}

	.pdf-name {
		width: 100%;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		font-size: 22rpx;
		color: #333;
	}

	.empty {
		position: relative;
		width: 10rem;
		height: 10rem;
		background: url('/static/empty.png') no-repeat;
		background-size: 100% auto;
		object-fit: cover;
		margin: 2rem auto;

		.empty-text {
			white-space: nowrap;
			position: absolute;
			bottom: -0.8rem;
			left: 50%;
			transform: translateX(-50%);
		}
	}

	.bottom_tool {
		position: fixed;
		width: 750rpx;
		background-color: rgba(255, 255, 255, 0.9);
		box-shadow: 0px 0px 0px 1px rgba(0, 0, 0, 0.08);
		left: 0px;
		bottom: 0px;
	}

	.tool_box {
		width: 750rpx;
		height: 50px;
		display: flex;
		align-items: center;
		flex-direction: row;
		justify-content: space-around;
	}
</style>